﻿Imports System.ComponentModel
Imports IICBusinessLogic
Imports CrystalReportViewer

Namespace Forms.List
    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    Public Class frmPeriodList
        Inherits frmBaseGrid


#Region "Designer"

        Public Sub setDiplomaSuperType(ByVal DiplomaSuperType As DiplomaSuperType)

            CType(Me.grdData, Grid.CGSQLGrid).gpWhereclause = "DiplomaSuperTypeId=" & DiplomaSuperType.DiplomaSuperTypeId

        End Sub

        'Form overrides dispose to clean up the component list.
        <System.Diagnostics.DebuggerNonUserCode()> _
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            Try
                If disposing AndAlso components IsNot Nothing Then
                    components.Dispose()
                End If
            Finally
                MyBase.Dispose(disposing)
            End Try
        End Sub

        Friend WithEvents UcListPeriod As ucPeriodList

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> _
        Private Sub InitializeComponent()
            Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmPeriodList))
            Me.UcListPeriod = New IIC.ucPeriodList()
            Me.pnlGrid.SuspendLayout()
            Me.SuspendLayout()
            '
            'pnlGrid
            '
            Me.pnlGrid.Controls.Add(Me.UcListPeriod)
            resources.ApplyResources(Me.pnlGrid, "pnlGrid")
            '
            'UcListPeriod
            '
            resources.ApplyResources(Me.UcListPeriod, "UcListPeriod")
            Me.UcListPeriod.Name = "UcListPeriod"
            '
            'frmPeriodList
            '
            resources.ApplyResources(Me, "$this")
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.Name = "frmPeriodList"
            Me.ShowExcelButton = True
            Me.ShowSearch = True
            Me.Controls.SetChildIndex(Me.pnlGrid, 0)
            Me.pnlGrid.ResumeLayout(False)
            Me.ResumeLayout(False)

        End Sub

#End Region

#Region "Standard Code"

        Private Const STR_COL_IS_OPEN As String = "isOpen"
        Private Const STR_COL_PER_DATE As String = "PeriodDate"

        Private tsPerClose As New ToolStripButton
        Private tsPerOpen As New ToolStripButton
        Private tsPerProcess As New ToolStripButton
        Private mnPerClose As New ToolStripMenuItem
        Private mnPerOpen As New ToolStripMenuItem
        Private mnPerProcess As New ToolStripMenuItem

        Private Sub frmPeriodList_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
           Handles Me.Load

            Me.grdData.Columns(STR_COL_IS_OPEN).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
            Me.grdData.loadGrid()
            Me.grdData.Sort(Me.grdData.Columns(STR_COL_PER_DATE), ListSortDirection.Descending)

            Me.addToolbarActionButton("Close Period...", AddressOf ClosePeriod, tsPerClose, mnPerClose, , _
                                      My.Resources.Resources.ClosePeriod)

            Me.addToolbarActionButton("Open Period...", AddressOf OpenPeriod, tsPerOpen, mnPerOpen, , _
                                      My.Resources.Resources.OpenPeriod)

            Me.addToolbarActionButton("Process Period...", AddressOf ProcessPeriod, _
                                                tsPerProcess, mnPerProcess, , _
                                                 My.Resources.Resources.Flag)

            AddHandler Me.grdData.CellClick, AddressOf grdDataCellClick

            If Me.grdData.CurrentRow IsNot Nothing Then
                Me.setOpenCloseButtonState(Me.grdData.CurrentRow.Index)
            End If

            Dim r As ReportMatadata = My.Application.reportsCache.getSystemReportByKey("1")
            Me.addToReportMenu(r.DisplayName, AddressOf cmdReportClick, r.ReportKey)

            r = My.Application.reportsCache.getSystemReportByKey("2")
            Me.addToReportMenu(r.DisplayName, AddressOf cmdReportClick, r.ReportKey)

            r = My.Application.reportsCache.getSystemReportByKey("4")
            Me.addToReportMenu(r.DisplayName, AddressOf cmdReportClick, r.ReportKey)

            r = My.Application.reportsCache.getSystemReportByKey("5")
            Me.addToReportMenu(r.DisplayName, AddressOf cmdReportClick, r.ReportKey)

            r = My.Application.reportsCache.getSystemReportByKey("6")
            Me.addToReportMenu(r.DisplayName, AddressOf cmdReportClick, r.ReportKey)

            r = My.Application.reportsCache.getSystemReportByKey("10")
            Me.addToReportMenu(r.DisplayName, AddressOf cmdReportClick, r.ReportKey)

        End Sub

        Protected Sub cmdReportClick(ByVal sender As Object, ByVal e As EventArgs)

            Dim r As ReportMatadata = Nothing

            r = My.Application.reportsCache.getSystemReportByKey(CStr(CType(sender, ToolStripMenuItem).Tag))

            Dim cr As CRReportViewer = New CRReportViewer
            cr.ReportMetaData = r
            cr.setCriterionValue("PeriodId", Me.grdData.IdValue)

            cr.ShowReport()

        End Sub

        ''' <summary>
        ''' This function is common to all forms that inherit from class frmBaseGrid
        ''' It priovides a common name to the underlying grid control that shows the records
        ''' </summary>
        Public Overrides Function grdData() As org.codegen.win.controls.Grid.CGBaseGrid
            Return Me.UcListPeriod.grdPeriod
        End Function

        Protected Sub DeleteRecordConfirmed(ByVal sender As System.Object) Handles Me.gridDeleteRecordConfirmed

            Dim m As New PeriodDBMapper
            Dim mo As Period = m.findByKey(Me.grdData.IdValue)
            Call m.delete(mo)

        End Sub

#End Region
#Region "Custom Methods"
        Private Sub ProcessPeriod(ByVal sender As Object, ByVal e As EventArgs)


            Dim drv As DataRowView = CType(Me.grdData.BindingSource.Item(Me.grdData.SelectedRows(0).Index), DataRowView)
            Dim period As Period = PeriodDataUtils.loadFromDataRow(drv.Row)

            Dim pp As New UIProcessPeriod()
            pp.processPeriod(period)

        End Sub

        Private Sub OpenPeriod(ByVal sender As Object, ByVal e As EventArgs)

            If Me.grdData.SelectedRows.Count = 0 Then
                Call winUtils.MsgboxStop("Please select at least one period from the list")
            Else
                Dim drv As DataRowView = CType(Me.grdData.BindingSource.Item(Me.grdData.SelectedRows(0).Index), DataRowView)
                Dim period As Period = PeriodDataUtils.loadFromDataRow(drv.Row)

                'ask user to confirm
                If winUtils.MsgboxQuestion( _
                    "Are you sure you want to Open period " & period.PeriodDescription & "?") _
                            <> MsgBoxResult.Yes Then Exit Sub

                period.openPeriod()
                PeriodDataUtils.savePeriod(period)
                Me.grdData.requery()

                If Application.OpenForms("frmMain") IsNot Nothing Then
                    CType(Application.OpenForms("frmMain"), frmMain).ucSystemTree.refreshPeriod(period)
                End If

            End If

        End Sub

        Private Sub ClosePeriod(ByVal sender As Object, ByVal e As EventArgs)

            If Me.grdData.SelectedRows.Count = 0 Then
                Call winUtils.MsgboxStop("Please select at least one period from the list")
            Else
                Dim drv As DataRowView = CType(Me.grdData.BindingSource.Item(Me.grdData.SelectedRows(0).Index), DataRowView)
                Dim period As Period = PeriodDataUtils.loadFromDataRow(drv.Row)

                'ask user to confirm
                If winUtils.MsgboxQuestion( _
                    "Are you sure you want to Close period " & period.PeriodDescription & "?") _
                            <> MsgBoxResult.Yes Then Exit Sub

                period.closePeriod()
                PeriodDataUtils.savePeriod(period)
                Me.grdData.requery()

                If Application.OpenForms("frmMain") IsNot Nothing Then
                    CType(Application.OpenForms("frmMain"), frmMain).ucSystemTree.refreshPeriod(period)
                End If

            End If

        End Sub

        Private Sub setOpenCloseButtonState(ByVal rowIndex As Integer)

            Dim thisrow As DataGridViewRow = Me.grdData.Rows(rowIndex)

            If CInt(thisrow.Cells(STR_COL_IS_OPEN).Value) = 1 Then
                tsPerOpen.Enabled = False
                mnPerOpen.Enabled = False
            Else
                tsPerOpen.Enabled = True
                mnPerOpen.Enabled = True
            End If

            tsPerClose.Enabled = Not tsPerOpen.Enabled
            mnPerClose.Enabled = Not tsPerOpen.Enabled

            tsPerProcess.Enabled = tsPerClose.Enabled
            mnPerProcess.Enabled = tsPerClose.Enabled

        End Sub

        Private Sub grdDataCellClick(ByVal sender As Object, _
                                  ByVal e As DataGridViewCellEventArgs)

            If e.RowIndex <= -1 Then Exit Sub
            Me.setOpenCloseButtonState(e.RowIndex)

        End Sub

#End Region



    End Class

End Namespace

